﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using SmartParking.Bussiness;

namespace SmartParking.Web.Manager
{
    public partial class RolePermissions : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                (Master as Logger).Title = "Role Permissions";
                (Master as Logger).ActiveMenuItem = "RolePermissions";
                LoadRoles();
                LoadAssignPermissions();
                LoadAvailablePermissions();
            }
        }

        protected void LoadRoles()
        {
            SecurityManager.BindRoles(ref cboxRole);
        }

        protected void LoadAvailablePermissions()
        {
            SecurityManager.BindAvailablePermissions(ref lbxAvailablePermissions, Convert.ToInt32(this.cboxRole.SelectedValue));
        }

        protected void LoadAssignPermissions()
        {
            SecurityManager.BindAssignedPermissions(ref lbxAssignPermissions, Convert.ToInt32(this.cboxRole.SelectedValue));
        }

        protected void cboxRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.LoadAssignPermissions();
            this.LoadAvailablePermissions();
        }

        protected void btnMoveRight_Click(object sender, EventArgs e)
        {
            bool isSelected = false;
            foreach (ListItem permission in this.lbxAvailablePermissions.Items)
            {
                if (permission.Selected)
                {
                    SecurityManager.AddRolePermission(Convert.ToInt32(this.cboxRole.SelectedValue), Convert.ToInt32(permission.Value), Profile.ManagerID, Request.UserHostAddress);
                    isSelected = true;
                }
            }

            if (!isSelected)
            {
                this.lblError.Visible = true;
                this.lblError.Text = "<br/><br/>Please select a permission from available permissions list. <br/>";
            }
            else
            {
                this.LoadAssignPermissions();
                this.LoadAvailablePermissions();

                this.lblError.Visible = false;
            }
        }

        protected void btnMoveLeft_Click(object sender, EventArgs e)
        {
            bool isSelected = false;

            foreach (ListItem permission in this.lbxAssignPermissions.Items)
            {
                if (permission.Selected)
                {
                    SecurityManager.DeleteRolePermission(Convert.ToInt32(this.cboxRole.SelectedValue), Convert.ToInt32(permission.Value), Profile.ManagerID, Request.UserHostAddress);
                    isSelected = true;
                }
            }

            if (!isSelected)
            {
                this.lblError.Visible = true;
                this.lblError.Text = "<br/><br/>Please select a permission from assign permissions list. <br/>";
            }
            else
            {
                this.LoadAssignPermissions();
                this.LoadAvailablePermissions();

                this.lblError.Visible = false;
            }
        }
    }
}
